(function () {
    var contractId=jQuery('#contractId').val();
    var contractType=jQuery('#contractType').val();
//获取回款计划tabs页面数据 服务器分页
    var serverData = {};
    var	serverTable;
    jQuery(document).ready(function () {
        serverTable = jQuery('#scheduleTable').DataTable({
            "serverSide": true,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "get",
                "cache": false,
                "url": window.rootPath + '/ui/paySchedule/'+contractId+'/'+contractType+'/getData',
                "data": function (d) {
                    return $.extend({}, d, serverData);
                }
            },
            columns: [
                {
                    data: null
                },
                {
                    data: 'fund'
                },
                {
                    data: 'amount'
                },
                {
                    data:'payDate',
                    render: function (data, type, full, meta) {
                        if (data)
                            return moment(data).format('YYYY/MM/DD');
                        else
                            return '';
                    }
                },
                {
                    data:'memo'
                }
            ],
            "drawCallback": function (oSettings) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                    k++;
                }
            }
        });
    });
//获取回款历史tabs页面数据 服务器分页
    var serverData1 = {};
    var	serverTable1;
    jQuery(document).ready(function () {
        serverTable1 = jQuery('#payTable').DataTable({
            "serverSide": true,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "get",
                "cache": false,
                "url": window.rootPath + '/ui/payment/payHistory/'+contractId+'/'+contractType+'/getAllData',
                "data": function (d) {
                    return $.extend({}, d, serverData1);
                }
            },
            columns: [
                {
                    data: null
                },
                {
                    data: 'paidSum'
                },
                {
                    data: 'paidWay'
                },
                {
                    data:'paidDate',
                    render: function (data, type, full, meta) {
                        if (data)
                            return moment(data).format('YYYY-MM-DD');
                        else
                            return '';
                    }
                },
                {
                    data:'payMan'
                },
                {
                    data:'memo'
                }
            ],
            "drawCallback": function (oSettings) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                    k++;
                }

                serverTable1.off('click', 'tr');
                serverTable1.on('click', 'tr', function (event) {
                    event = event || window.event;
                    var target = event.target || event.scrElement;
                    var data = serverTable1.row(this).data();
                    if (target.className.indexOf('fa-pencil') != -1 || target.className.indexOf('editBtn') != -1) {

                        editPayHistory(data);
                    }
                    return;
                });
            }
        });
    });
//获取开票历史tabs页面数据 服务器分页
    var serverData2 = {};
    var	serverTable2;
    jQuery(document).ready(function () {
        serverTable2 = jQuery('#paymentTable').DataTable({
            "serverSide": true,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "get",
                "cache": false,
                "url": window.rootPath + '/ui/payment/paymentHistory/'+contractId+'/'+contractType+'/getAllData',
                "data": function (d) {
                    return $.extend({}, d, serverData2);
                }
            },
            columns: [
                {
                    data: null
                },
                {
                    data: null,
                    defaultContent: "",
                    render: function (data, type, full, meta) {
                        var operate = '<input type="checkbox" class="checkchild">';
                        return operate;

                    }
                },
                {
                    data: 'paymentSum'
                },
                {
                    data:'paymentDate',
                    render: function (data, type, full, meta) {
                        if (data)
                            return moment(data).format('YYYY-MM-DD');
                        else
                            return '';
                    }
                },
                {
                    data: 'paymentMan'
                },
                {
                    data:'memo'
                }
            ],
            "drawCallback": function (oSettings) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                    k++;
                }
            }
        });
    });
    //编辑开票历史
    jQuery('.editPaymentBtn').on('click', function () {
        var selects=getSelect("#paymentTable",1);
        if(selects.length>1){
            $.springLemonDialog({
                content: '只能编辑一条记录',// 确认文本
                clickButton: function (sender, modal, index) {// 按钮事件处理
                    $(this).closeDialog(modal);
                }
            });
        }
        else if(selects.length==0){
            $.springLemonDialog({
                content: '请选择要编辑的记录',// 确认文本
                clickButton: function (sender, modal, index) {// 按钮事件处理
                    $(this).closeDialog(modal);
                }
            });
        }else if(selects.length==1) {
            jQuery('#id1').val(selects[0].id);
            jQuery('#paymentSum1').val(selects[0].paymentSum);
            jQuery('#paymentMan1').val(selects[0].paymentMan);
            if(selects[0].paymentDate){
                var time=moment(selects[0].paymentDate).format('YYYY-MM-DD');
                jQuery('#paymentDate1').val(time);
            }
            jQuery('#taxRate').val(selects[0].taxRate);
            jQuery('#isInIncome').val(selects[0].isInIncome);
            jQuery('#taxes').val(selects[0].taxes);
            jQuery('#memo2').val(selects[0].memo);
            jQuery('#registPaymentEditModal').modal('show');
        }
    });
    ///下拉框选择信息////
    jQuery('#taxRate').trigger('change');///自动触发下拉框
    jQuery('#taxRate').change(function(){
        var paymentSum1= jQuery('#paymentSum1').val();
        var taxRate=jQuery(this).children('option:selected').val();
        var taxRates=(taxRate/100+1)/(taxRate/100);
        jQuery('#taxes').val((taxRates*paymentSum1).toFixed(2));
    });
    jQuery('#isInIncome').trigger('change');///自动触发下拉框
    jQuery('#isInIncome').change(function(){
        var isIncome=jQuery('#isInIncome').val();
        var paymentSum1= jQuery('#paymentSum1').val();
        if(isIncome=="否"){
            var taxRate = jQuery('#taxRate').val();
            var taxRates=(taxRate/100+1)/(taxRate/100);
            jQuery('#taxes').val((taxRates*paymentSum1).toFixed(2));
        }
    });
    ///编辑保存开票
    jQuery('.savePaymentBtn1').on('click', function () {
        if (jQuery("#registPaymentEditForm") && !jQuery("#registPaymentEditForm").Validform({
                tiptype : 3,
                showAllError : true
            }).check(false)) {
            return;
        };
        var id = jQuery('#id1').val();
        var memo = jQuery('#memo2').val();
        var taxRate = jQuery('#taxRate').val();
        var isInIncome = jQuery('#isInIncome').val();
        var taxes=jQuery('#taxes').val();
        var contractId=jQuery('#contractId').val();
        var contractType=jQuery('#contractType').val();
        var url = window.rootPath + "/api/paymentRegists/" +id;
        var  d = {
                "taxRate": taxRate,
                "isInIncome": isInIncome,
                "taxes": taxes,
                "memo": memo,
                "contractId": contractId,
                "contractType":contractType
            };
        jQuery(".savePaymentBtn1").attr("disabled", true);
            jQuery.ajax({
                type: 'patch',
                url: encodeURI(url),
                data: JSON.stringify(d),
                dataType: 'json',
                contentType: 'application/json;charset=utf-8',
                success: function (data) {
                    jQuery('#registPaymentEditModal').modal('hide');
                    serverTable2.draw(false);
                    serverTable4.draw(false);
                    jQuery(".savePaymentBtn1").attr("disabled", false);
                }
            });
        });
//获取回款-计划tabs页面数据 服务器分页
    var serverData3 = {};
    var	serverTable3;
    jQuery(document).ready(function () {
        serverTable3 = jQuery('#paidScheduleTable').DataTable({
            "serverSide": true,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "get",
                "cache": false,
                "url": window.rootPath + '/ui/payment/payPlan/'+contractId+'/'+contractType+'/getData',
                "data": function (d) {
                    return $.extend({}, d, serverData3);
                }
            },
            columns: [
                {
                    data: null
                },
                {
                    data: 'fund'
                },
                {
                    data: 'scheduleSum'
                },
                {
                    data:'paidStage'
                },
                {
                    data:'schedulePayDate',
                    render: function (data, type, full, meta) {
                        if (data)
                            return moment(data).format('YYYY/MM/DD');
                        else
                            return '';
                    }
                },
                {
                    data:'deviationDate'
                },
                {
                    data:'memo'
                }
            ],
            "drawCallback": function (oSettings) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                    k++;
                }
            }
        });
    });
//获取开票-计划tabs页面数据 服务器分页
    var serverData4 = {};
    var	serverTable4;
    jQuery(document).ready(function () {
        serverTable4 = jQuery('#paymentScheduleTable').DataTable({
            "serverSide": true,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "get",
                "cache": false,
                "url": window.rootPath + '/ui/payment/paymentPlan/'+contractId+'/'+contractType+'/getData',
                "data": function (d) {
                    return $.extend({}, d, serverData4);
                }
            },
            columns: [
                {
                    data: null
                },
                {
                    data: 'fund'
                },
                {
                    data:'paymentStage'
                },
                {
                    data:'schedulePayDate',
                    render: function (data, type, full, meta) {
                        if (data)
                            return moment(data).format('YYYY/MM/DD');
                        else
                            return '';
                    }
                },
                {
                    data:'memo'
                }
            ],
            "drawCallback": function (oSettings) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                    k++;
                }
            }
        });
    });
})();